#coding:utf-8
'''
* coder  : dzlua
* email  : 505544956@qq.com
* module : ApiMgr-server
* path   : app/models
* file   : auth.py
* time   : 2017-11-08 10:07:08
'''
#--------------------#
from app.mgr import db
from app.mgr import auth
#--------------------#

#--------------------#
class User(db.Model):
    __tablename__ = 'user'
    #----------#
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(32), unique=True, nullable=False, index=True)
    password = db.Column(db.String(128), nullable=False)
    email = db.Column(db.String(50), unique=True, nullable=True, index=True)
    phone = db.Column(db.String(11), unique=True, nullable=True, index=True)
    email_verifyed = db.Column(db.Boolean, default=False)
    phone_verifyed = db.Column(db.Boolean, default=False)
    #----------#
    @staticmethod
    def get(**kwargs):
        return User.query.filter_by(**kwargs).first()
    #----------#
    @staticmethod
    def add(**kwargs):
        data = kwargs
        #
        password = kwargs.get('password')
        pwd = auth.decode_password(password)
        data['password'] = pwd
        #
        user = User(**data)
        db.session.add(user)
        db.session.commit()
    #----------#
    def update(self):
        db.session.commit()
#--------------------#
